package com.bw.usercenter.ui


import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Display
import android.view.View
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.ashokvarma.bottomnavigation.TextBadgeItem
import com.bw.net.netutils.NetUtils
import com.bw.usercenter.R
import com.bw.usercenter.adapter.MyAdapterFragemtn
import com.bw.usercenter.ui.fragment.HomeFragment
import com.bw.usercenter.ui.fragment.SortFragment

import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_home.*
import org.jetbrains.anko.toast

class MainActivity : AppCompatActivity(),BottomNavigationBar.OnTabSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var haneNet = NetUtils.getInstance().HaneNet(this@MainActivity)
        if (haneNet){
            toast("有网络")
        }else{
            toast("无网络")
        }

        var fragment:ArrayList<Fragment>  = arrayListOf()
        fragment.add(HomeFragment())
        fragment.add(SortFragment())
        fragment.add(SortFragment())
        fragment.add(SortFragment())
        fragment.add(SortFragment())

        val myadapter:MyAdapterFragemtn =MyAdapterFragemtn(this@MainActivity.supportFragmentManager,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,fragment)
        main_viewpager.adapter = myadapter

        main_viewpager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int,
            ) {

            }

            override fun onPageSelected(position: Int) {
                main_bottomnav.selectTab(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
        main_bottomnav.setTabSelectedListener(this@MainActivity)
            .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)
            .setMode(BottomNavigationBar.MODE_FIXED)
            .setBarBackgroundColor("#FB3C3C")
            .setInActiveColor("#2C2C2C")
            .setActiveColor("#F9FAFB")
        var textcolor:TextBadgeItem = TextBadgeItem()
        textcolor.setBackgroundColor(Color.RED).setText("99+").setTextColor(Color.WHITE)
        main_bottomnav.addItem(BottomNavigationItem(R.drawable.main,"刷新"))
            .addItem(BottomNavigationItem(R.drawable.sort,"分类"))
            .addItem(BottomNavigationItem(R.drawable.find,"发现").setBadgeItem(textcolor))
            .addItem(BottomNavigationItem(R.drawable.shopcart,"购物车"))
            .addItem(BottomNavigationItem(R.drawable.my,"我的"))
            .initialise()
    }

    override fun onTabSelected(position: Int) {
        main_viewpager.setCurrentItem(position)
    }

    override fun onTabUnselected(position: Int) {

    }

    override fun onTabReselected(position: Int) {

    }
}